#include<bits/stdc++.h>
#define gc getchar
#define itn int
#define x first
#define y second
#define eb emplace_back
#define em emplace
#define pb push_back
#define db double
#define y1 yy1_yyds
using namespace std; typedef long long ll; typedef unsigned long long ull;
// https://www.luogu.com.cn/discuss/522581 About "const"
ll read() {
	ll x = 0; short fh = 1; char ch = gc();
	while (!isdigit(ch)) {
		if (ch == '-') fh = -1;
		if (ch < 10) exit(0); 
		ch = gc();
	}
	while (isdigit(ch))
		x = x * 10 + (ch ^ 48), ch = gc();
	return fh * x;
}
#ifndef ONLINE_JUDGE
void debug() {cerr << "\n";}
template<typename Typ1> void debug(Typ1 arg) {cerr << arg << "\n";}
template<typename Typ1, typename ...Typ2> void debug(Typ1 arg, Typ2 ...args) {
	cerr << arg << " ", debug(args...);
}
#else
void debug() {}
template<typename Typ1> void debug(Typ1 arg) {}
template<typename Typ1, typename ...Typ2> void debug(Typ1 arg, Typ2 ...args) {}
#endif
void writeln(ll arg) {printf("%lld\n", arg);}
template<typename ...Typ2> void writeln(ll arg, Typ2 ...args) {
	printf("%lld ", arg), writeln(args...);
}
typedef pair <int, int> pii; typedef pair <ll, ll> pll;
const char Y_E_S[] = "YES", N__O[] = "NO";
// const char Y_E_S[] = "Yes", N__O[] = "No";
// #define infinite_testcase
// #define multiple_testcase
// #define output_Yes_No
const int DUST = 327, N = 214514, M = -1;
int input[N][2];
int fa[N], ch[N][2]/*, siz[N], dph[N]*/;
void pushup(int u) {}
bool chk(int x) {return ch[fa[x]][1] == x;}
void rotate(int x) {//草 是这样写的吗
	int y = fa[x], z = fa[y], k = chk(x), w = ch[x][k ^ 1];
	// debug(x, y, z, w);
	ch[y][k] = w, fa[w] = y;
	ch[z][chk(y)] = x, fa[x] = z;
	ch[x][k ^ 1] = y, fa[y] = x;
	pushup(y), pushup(x);
}
void splay(int x) {
	int y = fa[x];
	// while(x != 1 && (y = fa[x]) != 1) {
	while((y = fa[x])) {//fa[x] = 0: x = root
	/**/if(fa[y])
			chk(x) == chk(y) ? rotate(y) : rotate(x);
		rotate(x);
		// debug(x, fa[x]);
	}
}
const int mod = 1e9 + 7;//???
int ans[N];
void dfs(int x, int dph) {
	if(!x) return;
	(ans[x] += dph) %= mod;
	dfs(ch[x][0], dph + 1), dfs(ch[x][1], dph + 1);
}
bool major(int Case = 1) {
	int n = read();
	for(int i = 1; i <= n; i++) for(int j = 0; j < 2; j++) input[i][j] = read();
	for(int x = 1; x <= n; x++) {
		for(int i = 1; i <= n; i++) {
			ch[i][0] = input[i][0], ch[i][1] = input[i][1];
			fa[ch[i][0]] = fa[ch[i][1]] = i;
		}
	/**/fa[1] = 0;
		// for(int i = 1; i <= n; i++) debug(fa[i]);
		splay(x);
		dfs(x, 0);
	}
	for(int i = 1; i <= n; i++) writeln(ans[i]);
	return Case ^= Case ^ Case;
}
void initial_function(int argc, char **argv) {
	**argv = argc; /* <- place_holder
	you won't give up no matter what happens, will you?
	code time: 00:
	---
	不知道暴力写挂了没ww
	32 pts。
	*/
	freopen("dream.in", "r", stdin);
	freopen("dream.out", "w", stdout);
}
signed main(int argc, char **argv) {
	initial_function(argc, argv);
	int Case = 1, Maxcase = 1;
	for (
#ifdef multiple_testcase
		  Maxcase = read()
#endif
				     	  ;
#ifndef infinite_testcase
							Case <= Maxcase
#endif
				     					   ; Case++)
#ifdef output_Yes_No
		puts(major(Case) ? Y_E_S : N__O);
#else
		major(Case);
#endif
	return DUST ^ 0x147;
}
